package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.client.commentClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.formula.ptg.MemErrPtg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-12-22
 */
@RestController
@RequestMapping("/eduservice/comment")
//@CrossOrigin
public class EduCommentController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private commentClient commentClient;

    //根据课程id分页查询评论列表
    @GetMapping("getCommentInfo/{page}/{limit}")
    public R getCommentInfo(@PathVariable long page,@PathVariable long limit, String courseId){
        //创建page对象(分页)
        Page<EduComment> pageComment = new Page<>(page,limit);

        //构建条件
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        //排序（降序）
        wrapper.orderByDesc("gmt_create");
        wrapper.eq("course_id",courseId);

        //调用方法实现条件查询分页
        commentService.page(pageComment,wrapper);

        long total = pageComment.getTotal();//总记录数
        List<EduComment> records = pageComment.getRecords();//每页数据list集合
        long current = pageComment.getCurrent();//当前页
        long pages = pageComment.getPages();//总页数
        long size = pageComment.getSize();//每页记录数
        boolean hasNext = pageComment.hasNext();//当前是否有下一页
        boolean hasPrevious = pageComment.hasPrevious();//当前是否有上一页

        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("items",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return R.ok().data(map);
    }

    //添加评论
    @PostMapping("auth/save")
    public R addComment(@RequestBody EduComment comment, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)){
            System.out.println("清先登录");
            return R.error().code(28004).message("请先登录后再评论");
        }
        comment.setMemberId(memberId);

        UcenterMember ucenterInfo = commentClient.getMemberInfoById(memberId);

        comment.setNickname(ucenterInfo.getNickname());
        comment.setAvatar(ucenterInfo.getAvatar());

        commentService.save(comment);
        return R.ok();


    }
}

